package com.suwenhao.controller;



import com.suwenhao.enums.SessionEnum;
import com.suwenhao.utils.EmailUtil;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;


/**
 * @Author:suwenhao
 * @Description: 测试
 * @Date: Created in 2020/9/22 17:35
 */

@WebServlet("/email/*")
public class EmailServlet extends BaseServlet {


    protected void sendEmail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            String email = req.getParameter("email");

            //纯数字的验证码
            int result = (int) ((Math.random() + 1) * 1000);
            String code = String.valueOf(result);

            //包含数字和英文的验证码
            String ZiMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890";
//            String code = "";
//            Random random = new Random();
//            for (int i = 0; i < 4; i++) {
//                int index = random.nextInt(ZiMu.length());
//                char c = ZiMu.charAt(index);
//                code += c;
//            }


            //存session
            HttpSession session = req.getSession();
            session.setAttribute(SessionEnum.SESSION_FORGET.getValue(), code);
            session.setMaxInactiveInterval(60);

            EmailUtil.sendEmail(email, code);

            resp.getWriter().write("1");

        } catch (Exception e) {

            e.printStackTrace();

            resp.getWriter().write("0");
        }
    }

}


